/**
 * 
 */
package sudoku.ui;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import sudoku.util.Config;
import sudoku.util.Language;

/**
 * @author Romet
 *
 */
public class ComputerHelpUI implements ActionListener {

	private volatile SudokuUI m2ng;
	private volatile Config config;
	private volatile Language language;
	
	private JCheckBox computerchoice;
	private JCheckBox computerstatus;
	private JCheckBox computerhelp;
	private JCheckBox computerhinter;
	private JCheckBox computersolver;
	private JCheckBox computerchoicehelpremover;
	
	private JButton ok;
	private JButton cancel;
	
	private JDialog dialog;
	
	public ComputerHelpUI(SudokuUI m2ng, Config config, Language language) {
		this.m2ng = m2ng;
		this.config = config;
		this.language = language;
		drawWindow();
	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(ok)){
			save();
			dialog.dispose();
			
		}
		else if (e.getSource().equals(cancel)) {
			dialog.dispose();
		}
	}
	
	public void drawWindow() {
		dialog = new JDialog(m2ng, language.getComputerhelp(), true);
		dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		JPanel panel = new JPanel(new GridLayout(7, 1));
		JPanel panel2 = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		panel2.add(new JLabel(language.getComputerhelp_nb1()), c);
		c.gridx = 0;
		c.gridy = 1;
		panel2.add(new JLabel(language.getComputerhelp_nb2()), c);
		panel.add(panel2);
		computerchoice = new JCheckBox(language.getComputerhelpcomputer_choice());
		computerchoice.setSelected(config.isComputerchoice());
		panel.add(computerchoice);
		computerchoice.addActionListener(this);
		computerstatus = new JCheckBox(language.getComputerhelp_status());
		computerstatus.setSelected(config.isComputerstatus());
		panel.add(computerstatus);
		computerhelp = new JCheckBox(language.getComputerhelp_help());
		computerhelp.setSelected(config.isComputerhelp());
		panel.add(computerhelp);
		computerhinter = new JCheckBox(language.getComputerhelp_hinter());
		computerhinter.setSelected(config.isComputerhinter());
		panel.add(computerhinter);
		computersolver = new JCheckBox(language.getComputerhelp_solver());
		computersolver.setSelected(config.isComputersolver());
		panel.add(computersolver);
		computerchoicehelpremover = new JCheckBox(language.getComputerhelp_choicehelpremover());
		computerchoicehelpremover.setSelected(config.isComputerchoicehelpremover());
		panel.add(computerchoicehelpremover);
		JPanel p = new JPanel(new GridLayout(1, 2));
		ok = new JButton("OK");
		ok.addActionListener(this);
		cancel = new JButton(language.getButton_cancel());
		cancel.addActionListener(this);
		p.add(ok);
		p.add(cancel);
		dialog.add(panel, BorderLayout.CENTER);
		dialog.add(p, BorderLayout.SOUTH);
		dialog.pack();
		dialog.setLocation(m2ng.getLocation().x+m2ng.getWidth()/2-dialog.getWidth()/2, m2ng.getLocation().y+m2ng.getHeight()/2-dialog.getHeight()/2);
		dialog.setVisible(true);
	}
	
	public void save() {
		config.setComputerchoice(computerchoice.isSelected());
		config.setComputerstatus(computerstatus.isSelected());
		config.setComputerhelp(computerhelp.isSelected());
		config.setComputerhinter(computerhinter.isSelected());
		config.setComputersolver(computersolver.isSelected());
		config.setComputerchoicehelpremover(computerchoicehelpremover.isSelected());
		config.salvesta();
	}
}
